﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Configuration;

namespace InhollandAir.App_Code
{
    public class Airport
    {
        private int mId;
        private string mCountry;
        private string mCity;

        public void LoadFromDb(int airportId)
        {
            //get flight
            var connUser = new SqlConnection(WebConfigurationManager.ConnectionStrings["FlightReservation"].ToString());

            if (connUser.State == ConnectionState.Closed) connUser.Open();

            try
            {
                string sqlText = "SELECT * FROM airport WHERE airportID = @airportId";
                SqlCommand cmd = new SqlCommand(sqlText, connUser);
                cmd.Prepare();

                cmd.Parameters.AddWithValue("@airportId", airportId);

                SqlDataReader myReader = cmd.ExecuteReader();
                if (myReader.Read())
                {
                    this.mId = (int)myReader["airportID"];
                    this.mCountry = myReader["country"].ToString();
                    this.mCity = myReader["city"].ToString();
                }
                else
                {
                    throw new Exception("No airport found with airportID= " + id);
                }
            }
            catch (SqlException sqlEx)
            {
                throw sqlEx;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                connUser.Close();
            }
        }

        #region Properties
        public int id
        {
            get { return mId; }
            set { mId = value; }
        }

        public string country
        {
            get { return mCountry; }
            set { mCountry = value; }
        }
        public string city
        {
            get { return mCity; }
            set { mCity = value; }
        }
        #endregion
    }
}